package pacBiopackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;


public class PacBioKmerCheckMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		GetOpt go = new GetOpt(args, "o:a:s:h");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
	//	String filenameMarker = "";                 
		String filenameAlignment = "";   
		String filenameOutput="output.txt";
		String filenameStats="distanceStatistics.txt";
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'a') {
		    	filenameAlignment = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='s')
		    {
		    	filenameStats = go.optArgGet();
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameAlignment.isEmpty()) {
		    System.out.println("Usage: -a Alignment [ -o outputFile -s distanceStatistic-h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-a " + filenameAlignment); //NOI18N
		System.out.println("-o " + filenameOutput);
		System.out.println("-s " + filenameStats);
		System.out.println("------------------------");
		
		// now we have to correct each 
		BufferedReader br = null;
		try
		{
			br = new BufferedReader(new FileReader(filenameAlignment));
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput)));
			PacBioKmerCheck kmerCheck = new PacBioKmerCheck();
			kmerCheck.init();
			while(br.ready())
			{
				//test if we are getting a copy of br or if its counting up
				PacBioReadContainer read = new PacBioReadIntoContainer().readIntoContainer(br);
				//System.out.println(read.toString());
				List<Integer> validkmerList=kmerCheck.checkForKmer(read);
				pw.println(read.getHeader());
				if(validkmerList.size()>0)
				{
					pw.println(validkmerList.size());
					for(int i =0; i < validkmerList.size();++i)
					{
						pw.print(read.getPositionsList().get(validkmerList.get(i))+" ");
					}
					pw.println();
				}
				else
				{
					pw.println(0);
				}
			}
			pw.flush();
			pw.close();
			pw = new PrintWriter(new BufferedWriter(new FileWriter(filenameStats)));
			
			int counter=0;
			for(Integer distance: kmerCheck.getDistanceDistribution().keySet() )
			{
				/*	while(counter+1<distance)
				{
					++counter;
					pw.println(counter+"\t"+0);
				}
				*/
				pw.println(distance + "\t"+ kmerCheck.getDistanceDistribution().get(distance));
				++counter;
			}
			pw.flush();
			pw.close();			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
